/**
 * @Author: chensj
 * @Date: 18-11-21 下午8:45
 * cookie 使用
 */
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

const app = express();

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
//app.use(cookieParser()); //设置cookie
app.use(cookieParser("signed")); //使用signed加密cookies

app.get('/',function (req,res) {
    console.log(req.signedCookies);
    res.send('hello express');
});

app.get('/lvyou',function (req,res) {
    let city = req.query.city;
    let citys = req.signedCookies.citys;
    console.log(citys);
    if(citys){
        citys.push(city);
    }else{
        citys = [];
        citys.push(city);
    }
    res.cookie('city',city,{maxAge:500000,httpOnly:true,signed:true});
    res.cookie('citys',citys,{maxAge:500000,httpOnly:true,signed:true});
    res.send('旅游的城市:'+city);
});

app.get('/city',function (req,res) {
   let city = req.signedCookies.citys;
   res.send('已经旅游的城市:'+city);
});

app.listen(3001,'localhost');